package com.kol.collect.common.util;

import java.io.IOException;
import java.lang.reflect.Type;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;

public class NumberSerializer implements ObjectSerializer {

    private static volatile NumberSerializer numberSerializer;

    private NumberSerializer() {
    }

    public static NumberSerializer getInstance() {
        if (null == numberSerializer) {
            synchronized (NumberSerializer.class) {
                if (null == numberSerializer) {
                    numberSerializer = new NumberSerializer();
                }
            }
        }
        return numberSerializer;
    }

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
            throws IOException {
        SerializeWriter out = serializer.getWriter();
        if (object == null) {
            serializer.getWriter().writeNull();
            return;
        }
        if (object instanceof Float) {
            out.write(String.valueOf((Float) object));
        }
        if (object instanceof Double) {
            out.write(String.valueOf((Double) object));
        }
    }

}
